package com.app.controller;

import com.app.model.SpecialityManageModel;
import com.app.model.TrainSpecialityModel;
import com.app.service.SpecialityManageService;
import com.app.service.TrainSpecialityService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.vo.SpecialityManageVo;
import com.app.vo.IdNameChildrenVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//专业管理
@RestController
@Controller
@RequestMapping("/jiuquan/speciality")
public class SpecialityManageController {
    @Autowired
    private SpecialityManageService specialityManageService;

    @Autowired
    private TrainSpecialityService trainSpecialityService;

    //查询列表
    @GetMapping("/getSpecialityManageList")
    public Result getSpecialityManageList(@RequestParam(defaultValue = "0") Integer page,
                                          @RequestParam(defaultValue = "10") Integer limit, String keyword) {
        SpecialityManageVo SpecialityManageList = specialityManageService.getSpecialityManageList(page, limit, keyword);
        return Result.success(SpecialityManageList);
    }

    //详情
    @GetMapping("/getSpecialityManageById")
    public Result getSpecialityManageById(Integer id) {
        SpecialityManageModel SpecialityManage = specialityManageService.getById(id);
        return Result.success(SpecialityManage);
    }

    //新增和修改
    @PostMapping("/addAndUpdateSpeciality")
    public Result addAndUpdateSpeciality(@RequestBody SpecialityManageModel SpecialityManageModel) {
        SpecialityManageModel SpecialityManage = specialityManageService.getById(SpecialityManageModel.getId());
        if (SpecialityManage != null) {
            SpecialityManageModel.setUpdateTime(CommonHelp.getNowTimestamp());
            specialityManageService.updateById(SpecialityManageModel);
            return Result.success("修改成功");
        } else {
            SpecialityManageModel.setCreateTime(CommonHelp.getNowTimestamp());
            specialityManageService.save(SpecialityManageModel);
            return Result.success("添加成功");
        }
    }

    //删除
    @GetMapping("/delSpecialityManage")
    public Result delSpecialityManage(Integer id) {
        QueryWrapper<TrainSpecialityModel> qw = new QueryWrapper<>();
        qw.eq("speciality_id", id);
        List<TrainSpecialityModel> specialityList = trainSpecialityService.list(qw);
        if (specialityList.size() != 0) {
            return Result.error(405,"该专业已有学生报名,无法删除");
        }
        specialityManageService.removeById(id);
        return Result.success("删除成功");
    }

    //根据培训学校ID查专业列表
    @GetMapping("/getSpecialityByTrainId")
    public Result getSpecialityByTrainId(Integer trainId) {
        List<IdNameChildrenVo> specialityByTrainId = specialityManageService.getSpecialityByTrainId(trainId);
        return Result.success(specialityByTrainId);
    }
}
